import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const targetDir = path.join(__dirname, 'packages');
const excludeDirs = ['dist'];
const exts = ['.js', '.ts', '.vue'];

function countLines(filePath) {
  const content = fs.readFileSync(filePath, 'utf-8');
  return content.split('\n').length;
}

function walk(dir) {
  let total = 0;
  const files = fs.readdirSync(dir);
  for (const file of files) {
    if (excludeDirs.includes(file)) continue;
    const fullPath = path.join(dir, file);
    const stat = fs.statSync(fullPath);
    if (stat.isDirectory()) {
      total += walk(fullPath);
    } else if (exts.includes(path.extname(file))) {
      total += countLines(fullPath);
    }
  }
  return total;
}

const totalLines = walk(targetDir);
console.log(`Total lines: ${totalLines}`);